package com.ct.captcha.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.ct.captcha.model.CaptchaProperties;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    private static final long CAPTCHA_TIME_OUT = 30 * 60 * 1000;
    @Autowired
    private CaptchaProperties captchaProperties;
    @RequestMapping("/getCaptcha")
    public void createCaptcha (HttpServletResponse response, HttpSession session) {
        try {
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight());
            String code = lineCaptcha.getCode();
            // 进行空指针判断
            session.setAttribute(captchaProperties.getSession().getCode(), code);
            session.setAttribute(captchaProperties.getSession().getTime(), System.currentTimeMillis());
//            System.out.println(session.getId());
//            System.out.println("Session Code: " + session.getAttribute(captchaProperties.getS().getCode())); //有值
//            System.out.println("Session Code Key: " + captchaProperties.getS().getCode());
//            System.out.println("Session Time Key: " + captchaProperties.getS().getTime());
            System.out.println("生成验证码: " + code);
            response.setContentType("image/jpeg");
//        response.setContentType("image/png");
            lineCaptcha.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            System.out.println("发生异常: " + e);
        }
    }

    @RequestMapping("/check")
    public Boolean check (String captcha, HttpSession session) {
        // 参数校验
        if (!StringUtils.hasLength(captcha)) {
            return false;
        }
//        System.out.println(captchaProperties.getS().getCode()); //有值
//        System.out.println("Session Code: " + session.getAttribute(captchaProperties.getS().getCode()));
//        System.out.println("Session Time: " + session.getAttribute(captchaProperties.getS().getTime()));
//        System.out.println(session.getId());

        // 验证码校验
        Long captchaTime = (Long) session.getAttribute(captchaProperties.getSession().getTime());
        String captchaCode = (String) session.getAttribute(captchaProperties.getSession().getCode());
//        System.out.println(captchaCode);
        // 1. 时间校验d
        if (!StringUtils.hasLength(captchaCode) || captchaTime == null || (System.currentTimeMillis() - captchaTime > CAPTCHA_TIME_OUT)) {
            return false;
        }
        // 2.验证码内容校验
        if (captchaCode.equalsIgnoreCase(captcha)) {
            return true;
        }
        return false;
    }
}
